package com.itmate.jia;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class dologin
 */
@WebServlet("/dologin")
public class dologin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dologin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String issave=request.getParameter("issave");
		if("on".equals(issave)){
			Cookie c=new Cookie("username",username);
			c.setMaxAge(60*60*24);
			Cookie c2=new Cookie("password",password);
			c2.setMaxAge(60*60*24);
			response.addCookie(c);
			response.addCookie(c2 );
			
		}
		if("admin".equals(username)&&"admin".equals(password)){
			request.getSession().setAttribute("name", username);
			response.sendRedirect(request.getContextPath()+"/success.jsp");
		}else {
			response.sendRedirect(request.getContextPath()+"/fail.jsp");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
